﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObjectModel.PowerPoint
{
    public class LinkedObjectCollectionController
    {
        LinkedObjectCollection mLinkedObjects;
        
        public LinkedObjectCollectionController(LinkedObjectCollection linkedObjects)
        {
            mLinkedObjects = linkedObjects;
            mLinkedObjects.CollectionChanged += OnCollectionChanged;
        }

        void OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    foreach (object o in e.NewItems)
                        ((LinkedObject)o).Target.Deleted += Target_Deleted;
                    break;

                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    foreach (object o in e.OldItems)
                        ((LinkedObject)o).Target.Deleted -= Target_Deleted;
                    break;
            }
        }

        void Target_Deleted(object sender, EventArgs e)
        {
            LinkedObject itemToRemove = mLinkedObjects.First(o => o.Target == sender);
            mLinkedObjects.Remove(itemToRemove);
        }

        //void Target_SelectedExclusively(object sender, EventArgs e)
        //{
        //    LinkedObject selItem = this.First(o => o.Target == sender);
        //    SelectedItem = selItem;
        //}
    }
}
